#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int revolve(vector<int>& nums)
    {
        int left = 0;
        int right = nums.size() - 1;
        int mid;
        int n = nums[0];

        while (left <= right)
        {
            mid = (left + right) / 2;
            if (n == nums[mid])
            {
                if (mid == right)
                    return mid;
                else if (nums[mid] < nums[mid + 1])
                    return mid;
                else
                    return mid + 1;
            }
            else if (n > nums[mid])
            {
                if (nums[mid - 1] > nums[mid])
                    return mid;
                else
                {
                    right = mid - 1;
                }
            }
            else
                left = mid + 1;
        }
        return 0;
    }

    int binarysort(vector<int>& nums, int left, int right, int target)
    {
        int mid;
        while (left <= right)
        {
            mid = (left + right) / 2;
            if (target == nums[mid])
                return mid;
            else if (target < nums[mid])
                right = mid - 1;
            else
                left = mid + 1;
        }
        return -1;
    }
    int search(vector<int>& nums, int target) {
        int k = revolve(nums);
        int left, right;
        if (target == nums[0])
            return 0;
        else if (target < nums[0] || k == 0)
        {
            left = k;
            right = nums.size() - 1;
        }
        else
        {
            left = 1;
            right = k - 1;
        }
        return binarysort(nums, left, right, target);
    }
};